﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;

namespace search
{
    public partial class Form1 : Form
    {

        private delegate void MyDelegate();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task task_baidu = new Task(() => { Search_baidu("http://www.baidu.com/s?wd=" + textBox1.Text); });
            Task task_bing = new Task(() => { Search_bing("https://cn.bing.com/search?form=MOZTSB&pc=MOZI&q=" + textBox1.Text); });
            task_baidu.Start();
            task_bing.Start();
        }
        private void Search_baidu(string url)
        {

            WebClient webClient = new WebClient();
            byte[] buffer = webClient.DownloadData(url);
            string response = Encoding.UTF8.GetString(buffer);
            string realdatar = "";
            string pattern = @"[\u4E00-\u9FFF]+";
            MatchCollection mc = Regex.Matches(response, pattern);
            foreach (Match m in mc)
            {
                realdatar += m;
            }
            this.BeginInvoke(new MyDelegate(() => { textBox_baidu.Text = realdatar.Substring(0, 200); }));


        }
        private void Search_bing(string url)
        {
            try
            {
                using (WebClient webClient = new WebClient() as WebClient)
                {
                    byte[] buffer = webClient.DownloadData(url);
                    string response = Encoding.UTF8.GetString(buffer);
                    string realdatar = "";
                    string pattern = @"[\u4E00-\u9FFF]+";
                    MatchCollection mc = Regex.Matches(response, pattern);
                    foreach (Match m in mc)
                    {
                        realdatar += m;
                    }
                    this.BeginInvoke(new MyDelegate(() => { textBox_bing.Text = realdatar.Substring(0, 200); }));
                }
            }
            catch (Exception)
            {
                MessageBox.Show("不支持该请求！");
            }
        }
    }
}
